package com.diet.web.controller.diet;

import com.diet.common.annotation.Log;
import com.diet.common.core.controller.BaseController;
import com.diet.common.core.domain.AjaxResult;
import com.diet.common.core.page.TableDataInfo;
import com.diet.common.enums.BusinessType;
import com.diet.common.utils.poi.ExcelUtil;
import com.diet.system.domain.Dish;
import com.diet.system.service.IDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 菜品Controller
 */
@RestController
@RequestMapping("/diet/dish")
public class DishController extends BaseController
{
    @Autowired
    private IDishService dishService;

    /**
     * 查询菜品列表
     */
    @PreAuthorize("@ss.hasPermi('diet:dish:list')")
    @GetMapping("/list")
    public TableDataInfo list(Dish dish)
    {
        startPage();
        List<Dish> list = dishService.selectDishList(dish);
        return getDataTable(list);
    }

    /**
     * 导出菜品列表
     */
    @PreAuthorize("@ss.hasPermi('diet:dish:export')")
    @Log(title = "菜品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Dish dish)
    {
        List<Dish> list = dishService.selectDishList(dish);
        ExcelUtil<Dish> util = new ExcelUtil<Dish>(Dish.class);
        util.exportExcel(response, list, "菜品数据");
    }

    /**
     * 获取菜品详细信息
     */
    @PreAuthorize("@ss.hasPermi('diet:dish:query')")
    @GetMapping(value = "/{dishId}")
    public AjaxResult getInfo(@PathVariable("dishId") Long dishId)
    {
        System.out.println("获取菜品详细信息" + dishId);
        System.out.println("获取菜品详细信息" + dishService.selectDishByDishId(dishId));
        return AjaxResult.success(dishService.selectDishByDishId(dishId));
    }

    /**
     * 新增菜品
     */
    @PreAuthorize("@ss.hasPermi('diet:dish:add')")
    @Log(title = "菜品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dish dish)
    {
        return toAjax(dishService.insertDish(dish));
    }

    /**
     * 修改菜品
     */
    @PreAuthorize("@ss.hasPermi('diet:dish:edit')")
    @Log(title = "菜品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dish dish)
    {
        return toAjax(dishService.updateDish(dish));
    }

    /**
     * 删除菜品
     */
    @PreAuthorize("@ss.hasPermi('diet:dish:remove')")
    @Log(title = "菜品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dishIds}")
    public AjaxResult remove(@PathVariable Long[] dishIds)
    {
        return toAjax(dishService.deleteDishByDishIds(dishIds));
    }

    /**
     * 获取菜品推荐列表
     */
    @GetMapping("/recommend")
    public TableDataInfo recommend(Dish dish, String userId)
    {
        startPage();
        List<Dish> list = dishService.selectDishList(dish);
        return getDataTable(list);
    }

    /**
     * 获取热门菜品
     */
    @GetMapping("/hot")
    public TableDataInfo getHotDishes(@RequestParam(defaultValue = "rating") String sortType)
    {
        startPage();
        List<Dish> list = dishService.selectHotDishList(sortType);
        return getDataTable(list);
    }

    /**
     * 搜索菜品
     */
    @GetMapping("/search")
    public TableDataInfo search(@RequestParam String keyword)
    {
        startPage();
        List<Dish> list = dishService.searchDishList(keyword);
        return getDataTable(list);
    }
}